﻿
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using SanJ.FreeCare.ConditionDefinitions;
using SanJ.FreeCare.ObservationDefinitions;
using SanJ.FreeCare.ValueSets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;

namespace SanJ.FreeCare.Locations
{
    public class LocationManager : DomainService
    {
        protected ILocationRepository _locationRepository;
        public LocationManager(ILocationRepository locationRepository)
        {
            _locationRepository = locationRepository;
        }

        public async Task<List<Location>> GetList(string filter)
        {
           return await _locationRepository.GetListAsync(p => p.Name.Contains(filter) || p.Spell.Contains(filter));
        }
        public async Task<Location> GetByIdAsync(Guid? id)
        {
            return (await _locationRepository.GetListAsync(p => p.Id == id)).FirstOrDefault();
        }
        public Location CreateBed()
        {
            return null;
        }
        public async Task<Location>CreateAsync(Location location)
        {
            location.Code = await GetNextChildCodeAsync(location.ParentId);
            if (location.ParentId == Guid.Empty) 
            {
                location.ParentId = Guid.NewGuid();
            }
            if (location.IsVaild.IsNullOrEmpty())
            {
                location.IsVaild = VaildType.InVaild;
            }
            return await _locationRepository.InsertAsync(location);
        }

        private async Task<double> GetNextChildCodeAsync(Guid? parentId)
        {
            throw new NotImplementedException();
        }

        public async Task<List<Location>> GetPageLisAsync(string filter, string sorting, int maxResultCount = int.MaxValue,
            int skipCount = 0)
        {
            return await _locationRepository.GetPagedListAsync(filter,sorting,maxResultCount,skipCount);
        }

        public async Task<long> GetCountAsync(string? filterText)
        {
            return await _locationRepository.GetCountAsync(filterText);
        }
        public Task Delete(Guid id)
        { 
            return _locationRepository.DeleteAsync(id);
        }
        public async Task<Location>UpdateAsync(Guid id, Location location)
        {
            return await _locationRepository.UpdateAsync(location);
        }
        public Task UpdateBatchAsync(List<Location> location) 
        {
            return _locationRepository.UpdateManyAsync(location);
        }
        
    }
}